package pl.ydp.ivy.flex.ide.handlers;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;

import com.adobe.flexbuilder.project.actionscript.ActionScriptCore;

import pl.ydp.ivy.flex.ide.Activator;
import pl.ydp.ivy.flex.ide.WorkspaceIvyProjects;

public class RerieveAllJob extends Job {
	
	public RerieveAllJob(String name) {
		super(name);
	}

	@Override
	protected IStatus run(IProgressMonitor monitor) {
		WorkspaceIvyProjects ivyProjects = new WorkspaceIvyProjects( ResourcesPlugin.getWorkspace().getRoot() );
		IProject [] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
		
		for( IProject project: projects ) {
			if (	ActionScriptCore.getProject( project)!=null && 
					Activator.getDefault().getIvyProvider().isIvyPresent(project) ) {
				Job projectJob = createProjectJob( project, ivyProjects );
				projectJob.setRule( ResourcesPlugin.getWorkspace().getRoot() );
				projectJob.schedule();
			}
		}
		
		return Status.OK_STATUS;
	}

	protected Job createProjectJob(IProject project, WorkspaceIvyProjects prjcts) {
		return new RetrieveProjectJob( "Retrieve project: "+project.getName(), project, prjcts );
	}

}
